package com.example.juc.big_talk_concurrent.chapter6.article2;

import java.util.concurrent.Exchanger;

public class Test1 {
    public static void main(String[] args) throws InterruptedException {
        Exchanger<String> exchanger = new Exchanger<>();

        new Thread(new Runnable() {
            @Override
            public void run() {
                String strA = null;
                try {
                    strA = exchanger.exchange("大佬 A 的钥匙");
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                }
                // 阻塞住
                System.out.println("A: 获取" + strA);
            }
        }).start();

        new Thread(new Runnable() {
            @Override
            public void run() {
                String strB = null;
                try {
                    strB = exchanger.exchange("大佬 B 的钥匙");
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                }
                System.out.println("B: 获取" + strB);
            }
        }).start();


    }
}
